package com.bwie.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtil;
import org.bouncycastle.jcajce.provider.symmetric.IDEA;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.Menu;
import com.bwie.mapper.MenuMapper;
import com.bwie.service.MenuService;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 鑫仔
 * @Date: 2025/7/18 14:14
 * @Description:
 **/
@Transactional
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private HttpServletRequest request;

    @Override
    public R getMenuTree() {
        String token = request.getHeader("token");
        Integer userId = TokenUtil.getUserId(token);

        List<Menu> menuList = menuMapper.getMenuByUserId(userId);
        //菜单树
        List<Tree<String>> treeList = TreeUtil.build(menuList, "0", (menu, treeNode) -> {
            treeNode.setId(menu.getMenuId().toString());
            treeNode.setParentId(menu.getMenuPid().toString());
            treeNode.setName(menu.getMenuName());

            treeNode.put("path",menu.getMenuPath());

        });
        return R.OK(treeList);
    }
}
